package com.formacion.ipartek.migracion.ddbb;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import com.formacion.ipartek.migracion.interfaces.InterfacePersonaDAO;
import com.ipartek.formacion.migracion.dao.MySqlPersonaDAO;

/**
 * @author Fila3+1
 */
public class MySqlDAOFactory extends DAOFactory {
	// inicializo un DataSource (para utilizar el context.xml
	// Parametros configuracion base de datos
	private static final String DRIVER_MYSQL = "com.mysql.jdbc.Driver";
	private static String dbName;
	private static String dbPort;
	private static String dbUser;
	private static String dbPass;
	private static String dbServer;
	private static String dbUrl;

	private final static String PATH_CONFIG = "config/configuracion.properties";

	private static final Logger LOG = Logger.getLogger(MySqlDAOFactory.class);

	// Constructor
	// ************************************************************************
	public MySqlDAOFactory() {
		PropertyConfigurator.configure("config/log4j.properties");
		final Properties prop = new Properties();
		FileInputStream fis;
		try {
			fis = new FileInputStream(PATH_CONFIG);
			prop.load(fis);
			dbName = prop.getProperty("database");
			dbUser = prop.getProperty("dbuser");
			dbPass = prop.getProperty("dbpass");
			dbServer = prop.getProperty("dbserver");
			dbPort = prop.getProperty("dbport");
			dbUrl = "jdbc:mysql://" + dbServer + ":" + dbPort + "/" + dbName;
		} catch (final FileNotFoundException e) {

			e.printStackTrace();
		} catch (final IOException e) {

			e.printStackTrace();
		}

	}

	// Metodos
	// ************************************************************************

	// Conectar con la base de datos
	public static Connection conectar() throws Exception {
		Connection conexion = null; // Creamos un tipo connection
		try {
			Class.forName(DRIVER_MYSQL);

			// Define URL of database server for
			// database named mysql on the localhost
			// with the default port number 3306.
			// Get a connection to the database for a
			// user named root with a blank password.
			// This user is the default administrator
			// having full privileges to do anything.

			LOG.trace("Conectamos con la ddbb");
			dbUrl = "jdbc:mysql://" + dbServer + ":" + dbPort + "/" + dbName;
			conexion = DriverManager.getConnection(dbUrl, dbUser, dbPass);
			conexion.setAutoCommit(true);

			LOG.trace("Conectados con la ddbb " + dbUrl);
		} catch (final ClassNotFoundException e) {
			LOG.error("ClassNotFoundException driver no encontrado "
					+ DRIVER_MYSQL);
		} catch (SQLException e) {
			while (e != null) {
				LOG.error("Message: " + e.getMessage() + "SQLState: "
						+ e.getSQLState() + "ErrorCode: " + e.getErrorCode());
				e = e.getNextException();
			}
		} catch (final Exception e) {
			LOG.error("Exception general al conectar con la ddbb "
					+ e.getMessage());
		}
		LOG.trace("Devolvemos la conexion con la ddbb");
		return conexion;
	}

	// Desconectar con la base de datos
	public static void desconectar(final Connection conexion) {
		if (conexion != null) {
			try {
				conexion.close();
			} catch (final SQLException ex) {
				LOG.error("Exception general al desconectar con la ddbb "
						+ ex.getMessage());
			}
		}
	}

	@Override
	public InterfacePersonaDAO getInterfacePersonaDAO() {

		return new MySqlPersonaDAO("persona");
	}

}
